//+------------------------------------------------------------------+
//|                                                 SpreadTwoCcy.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright"
#property link "yeloparis@yahoo.co.uk" 
#property indicator_separate_window  
#property indicator_buffers 4
#property indicator_color1 Coral
#property indicator_color2 Snow
#property indicator_color3 Snow 
#property indicator_color4 SpringGreen//Tomato 
#property indicator_style1 STYLE_SOLID 
#property indicator_style2 STYLE_DASHDOTDOT
#property indicator_style3 STYLE_DASHDOTDOT
#property indicator_style4 STYLE_DOT
#property indicator_width1 2 
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1   

#define BARUP "BarUp"
#define SPREAD "Spread"
#define LABEL "label"
#define LABEL2 "label2"
#define LABELHIGH "labelHigh"
#define LABELSTOP "labelStop"
#define CORRELATION "Correlation"
#define ECARTLOW "EcartLOW"
#define ECARTHIGH "EcartHIGH"
#define SPREADIFF "SpreadDiff"
#define SPREADL "SpreadL"
#define TIME "Time"
#define ECART "Ecart"
#define MOVINGAVERAGE "Moving Average"
#define RETURNTOMA "ReturnToMA"
#define BANDSPERIOD "Bands Period"
#define ECARTSTOP "EcartStop"

//---- Currencies-------------------------------------------------------
extern string Synbol     = "EURUSD"; 
extern string SynbolCorr = "EURGBP";   
//---- indicator parameters---------------------------------------------
extern int    BandsPeriod=10;
extern double Ecart=0.002375;
extern double EcartStop=0.00375; 
extern int    LenghtEcartStop=15;    
extern int    LenghtEcart=10; 
extern double Deviation=0.75; 
extern int    MethodMA=0;// MODE_SMA 0 Simple moving average; MODE_EMA 1                       
extern int    MA_Shift=0; 
extern bool   Envelope = true;//if true will bild Enveloped else BBands 
static string short_name;
//---- buffers Lines----------------------------------------------------
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
double MovingBufferMovingAverage[]; 
int    ExtCountedBars=0; 
//-------------------------------------------------------------------------
int init()
   {
   string short_name;   
   /*RefreshRates();/*When expert advisor has been calculating for 
   a long time and needs data refreshing*/   
   IndicatorBuffers(4);//Allocates memory for buffers used for custom indicator calculations
   /*
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)); 
   */
   SetIndexStyle(0,DRAW_LINE); 
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle(1,DRAW_LINE);  
   SetIndexBuffer(1,UpperBuffer);
   SetIndexStyle(2,DRAW_LINE); 
   SetIndexBuffer(2,LowerBuffer);   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,MovingBufferMovingAverage);  
   
   IndicatorDigits(Digits+0);// +n Ajouter number required  
   /*
   SetIndexDrawBegin(0,MovingBuffer);/*Sets the bar number (from the data
   beginning) from which the drawing of given indicator line must start)*/
   /*
   SetIndexDrawBegin(0,UpperBuffer);
   SetIndexDrawBegin(0,LowerBuffer); 
   SetIndexDrawBegin(0,MovingBufferMovingAverage);    
   */  
   short_name="SPREAD & BANDS("+BandsPeriod+")"; 
   IndicatorShortName(short_name);//to be shown in the DataWindow (Chart) 
   
   SetIndexLabel(0,"Spread: MA ("+BandsPeriod+")" /*short_name*/);
   //Sets drawing line desc 4 showing in the DataWindow n in tooltip
   SetIndexLabel(1,"Upper Band"); 
   SetIndexLabel(2,"Lower Band"); 
   SetIndexLabel(3,"Moving Average");
   SetIndexLabel(4,"Ecart");
   
   if(Deviation<0.1) Deviation=0.1;
   if(Deviation>100.0) Deviation=10.0; 
  
   //----Will Show Info on Indicator-----------
   CreateInfoObjects();//added
   //------------------------------------------         
   return(0);
   }
//----------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   //----
   ObjectDelete(BARUP);
   ObjectDelete(SPREAD);
   ObjectDelete(LABEL);
   ObjectDelete(LABEL2);
   ObjectDelete(LABELHIGH);
   ObjectDelete(LABELSTOP);
   ObjectDelete(CORRELATION);
   ObjectDelete(ECARTLOW);
   ObjectDelete(ECARTHIGH);
   ObjectDelete(SPREADIFF);
   ObjectDelete(SPREADL);
   ObjectDelete(TIME);
   ObjectDelete(ECART);
   ObjectDelete(MOVINGAVERAGE);
   ObjectDelete(RETURNTOMA);
   ObjectDelete(BANDSPERIOD);
   ObjectDelete(ECARTSTOP);
   return(0);
}
  
int start() 
   {
//======Type your name==================================================
      /* 
      double currentMA;
      int j;      
      currentMA = iMA(0,PERIOD_H1,20,0,MODE_SMA,PRICE_TYPICAL,j);
      
      ObjectCreate("maValue",OBJ_LABEL,0,Time[j],currentMA);
      ObjectSetText("maValue",currentMA,8,"Tahoma",Gold);
      
      currentMA = iMA(0,PERIOD_H1,20,0,MODE_SMA,PRICE_TYPICAL,i);
      ObjectCreate("maValue"+i,OBJ_LABEL,0,Time[i],currentMA+0.0005);
      ObjectSetText("maValue"+i,currentMA,8,"Tahoma",Gold);
      */ 
//======================================================================     
   int counted_bars=IndicatorCounted();
   int i;
   int limit;    
   
   double deviation;
   double sum,oldval,newres;
//---- check for possible errors
   if(counted_bars<0) return(-1);         
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;   
//---- main loop      
   for(i=0; i<limit; i++)     
   {    
   if (iClose(SynbolCorr,0,i) != 0)
   {
   MovingBuffer[i]=MathLog(iClose(Synbol,0,i)/iClose(SynbolCorr,0,i));
   }    
//----Text about levels -------------------  
   if (ObjectFind(BARUP) == -1)
   { 
      ObjectCreate(BARUP, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(BARUP, OBJPROP_CORNER, 3);
      ObjectSet(BARUP, OBJPROP_XDISTANCE, 20);
      ObjectSet(BARUP, OBJPROP_YDISTANCE,10); 
   }
   ObjectSetText(BARUP, "Philippe Dadjo = "+Symbol()+"  "+MovingBuffer[i], 10, "Arial Black", Green);      
     
   if(WindowsTotal() > 1)  
   {  
      ObjectCreate(SPREAD, OBJ_LABEL, 1, 0, 0, 0, 0);
      ObjectSet(SPREAD, OBJPROP_CORNER, 1);
      ObjectSet(SPREAD, OBJPROP_XDISTANCE, 400); 
      ObjectSet(SPREAD, OBJPROP_YDISTANCE,30); 
      ObjectSetText(SPREAD, "Spread: "+"  "+MovingBuffer[0], 10, "Arial Black", Green);    
   }   
//----Comment seront HAUT Gauche -------------------------------------------------     
   /*
   double FreeMargin=AccountFreeMargin(); 
   Comment(""
   ,"\n",      "Account free margin: ",DoubleToStr(FreeMargin,2) 
   ,"\n",      ""
   ,"\n",      "Current time : ",TimeToStr(TimeCurrent()) 
   ,"\n",      ""
   ,"\n",      "Current time is of System     ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)
   ,"\n",      ""
   ,"\n",      "How are you Brother ",MovingBuffer[i] 
   ,"\n",      ""
   ,"\n",      "Current time is of System: ",TimeToStr(TimeCurrent(),TIME_SECONDS));  
   */
   }    
   
//--------------------Envelop Bands---------------------------------------------- 
   for(i=0; i<limit; i++)// not Bars     
   //for(i=limit; i>=0; i--)// not Bars   
   {    
      MovingBufferMovingAverage[i] =     iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);    
      if(Envelope) 
      {   
         UpperBuffer[i] = (1+Deviation/100)*iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);
         LowerBuffer[i] = (1-Deviation/100)*iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);
      }
      else
      {
      // Bollinger Bands calculations from Custom Indicators (Bands.mq4)
         sum=0.0;
         int k=i+BandsPeriod-1;
         oldval=MovingBufferMovingAverage[i];
         while(k>=i)
           {
            newres=MovingBuffer[k]-oldval;
            sum+=newres*newres;
            k--;
           }
         deviation=Deviation*MathSqrt(sum/BandsPeriod);
         UpperBuffer[i]=oldval+deviation;
         LowerBuffer[i]=oldval-deviation;
      // Bollinger Bands calculations from Custom Indicators (Bands.mq4)
      }         
      
   }
  
   /*
   LowerBuffer[i] =               iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_LOWER,i);
   UpperBuffer[i] =               iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_UPPER,i); 
   MovingBufferMovingAverage[i] = iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_MAIN,i);    
   */
   
   /*
   int   currentPeak=3.2;
   int   lastPeak=2.5;  
   DrawIndicatorTrendLine(Time[i], Time[i-1],MovingBufferMovingAverage[currentPeak],
         MovingBufferMovingAverage[lastPeak], Red, STYLE_DOT);
   */
  
//---------------------------------------------------------------------    
   /*
   Print("Account equity = ",AccountEquity());
   Print("Account free margin is ETSTTETTST ", AccountFreeMargin());
   */  
//---------------------------------------------------------------------      
   //----Incorrect-------------------
   /*
   ObjectCreate("Bar1", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("Bar1", OBJPROP_CORNER, 4);
   ObjectSet("Bar1", OBJPROP_XDISTANCE, 20);
   ObjectSet("Bar1", OBJPROP_YDISTANCE,30); 
   ObjectSetText("Bar1", "Philippe Dadjo = "+iClose(Synbol,0,i), 10, "Arial Black", Green);
   */
             
//==================================SELL LEVEL===================================            
   double      P1;                                
   P1 = MovingBufferMovingAverage[0]+Ecart;   
   if(WindowsTotal() > 1)  
   {
      if (ObjectFind(LABEL) == 1)
      {
          ObjectSet(LABEL, OBJPROP_PRICE1, P1);
          ObjectSet(LABEL, OBJPROP_PRICE2, P1);
          ObjectSet(LABEL, OBJPROP_TIME1, Time[0]);
          ObjectSet(LABEL, OBJPROP_TIME2, Time[LenghtEcart]);
      }
      else
      {
          ObjectCreate(LABEL, OBJ_TREND, 1, Time[0], P1, Time[LenghtEcart], P1);   
          ObjectSet(LABEL, OBJPROP_RAY, 0);
          ObjectSet(LABEL, OBJPROP_COLOR, Gold);
          ObjectSet(LABEL, OBJPROP_STYLE, STYLE_DOT);  
      }
   //==================================BUY LEVEL===================================   
      double      Pone;
      Pone = MovingBufferMovingAverage[0]-Ecart;    
   
  
      if (ObjectFind(LABEL2) == 1)
      {
          ObjectSet(LABEL2, OBJPROP_PRICE1, Pone);
          ObjectSet(LABEL2, OBJPROP_PRICE2, Pone);
          ObjectSet(LABEL2, OBJPROP_TIME1, Time[0]);
          ObjectSet(LABEL2, OBJPROP_TIME2, Time[LenghtEcart]);
      }
      else
      {
          ObjectCreate(LABEL2, OBJ_TREND, 1, Time[0], Pone, Time[LenghtEcart], Pone);  
          ObjectSet(LABEL2, OBJPROP_RAY, 0);
          ObjectSet(LABEL2, OBJPROP_COLOR, Gold);
          ObjectSet(LABEL2, OBJPROP_STYLE, STYLE_DOT);  
      }
   //=========================STOP HIGH====================================   
      double      P1Stop;                                
      P1Stop = MovingBufferMovingAverage[0]+EcartStop;     
     
      if (ObjectFind(LABELHIGH) == 1)
      {
          ObjectSet(LABELHIGH, OBJPROP_PRICE1, P1Stop);
          ObjectSet(LABELHIGH, OBJPROP_PRICE2, P1Stop);
          ObjectSet(LABELHIGH, OBJPROP_TIME1, Time[0]);
          ObjectSet(LABELHIGH, OBJPROP_TIME2, Time[LenghtEcartStop]);
      }
      else
      {
          ObjectCreate(LABELHIGH, OBJ_TREND, 1, Time[0], P1Stop, Time[LenghtEcartStop], P1Stop);   
          ObjectSet(LABELHIGH, OBJPROP_RAY, 0);
          ObjectSet(LABELHIGH, OBJPROP_COLOR, Red);
          ObjectSet(LABELHIGH, OBJPROP_STYLE, STYLE_DASH);   
      }
   //==========================STOP LOW===================================      
      double PoneStop;
      PoneStop = MovingBufferMovingAverage[0]-EcartStop;    
   
  
      if (ObjectFind(LABELSTOP) == 1)
      {
          ObjectSet(LABELSTOP, OBJPROP_PRICE1, PoneStop);
          ObjectSet(LABELSTOP, OBJPROP_PRICE2, PoneStop);
          ObjectSet(LABELSTOP, OBJPROP_TIME1, Time[0]);
          ObjectSet(LABELSTOP, OBJPROP_TIME2, Time[LenghtEcartStop]);
      }
      else
      {
          ObjectCreate(LABELSTOP, OBJ_TREND, 1, Time[0], PoneStop, Time[LenghtEcart], PoneStop);  
          ObjectSet(LABELSTOP, OBJPROP_RAY, 0);
          ObjectSet(LABELSTOP, OBJPROP_COLOR, Red);
          ObjectSet(LABELSTOP, OBJPROP_STYLE, STYLE_DASH);  
      }   
   } 
         
   CreateInfoObjects();      
   return(0);   
   }  
//=================After Being Initialized CreateInfoObjects=========================
void CreateInfoObjects()
{       
   double iecart,iecartstop;
   iecart = Ecart*10000;
   iecartstop=EcartStop*10000; 
   int NumRound = 3;
   int Xdist = 90; 
   int Xfont = 9; 
   int Ydist = 60;  

   if (ObjectFind(TIME) == -1)
   {
      ObjectCreate(TIME, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(TIME, OBJPROP_CORNER, 1);
      ObjectSet(TIME, OBJPROP_XDISTANCE, Xdist+11);
      ObjectSet(TIME, OBJPROP_YDISTANCE,Ydist-15);
   }  
   ObjectSetText(TIME, "Time:  "+TimeToStr(TimeCurrent(),TIME_SECONDS)+"  ", Xfont, "Arial Black", Snow);    
     
   if (ObjectFind(ECART) == -1)
   {
      ObjectCreate(ECART, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(ECART, OBJPROP_CORNER, 1);
      ObjectSet(ECART, OBJPROP_XDISTANCE, Xdist+23);
      ObjectSet(ECART, OBJPROP_YDISTANCE,Ydist);  
   }
   ObjectSetText(ECART, "Ecart Indic:  "+DoubleToStr(iecart,NumRound)+"  ", Xfont, "Arial Black",BurlyWood );    
   
   if (ObjectFind(ECARTSTOP) == -1)
   {
      ObjectCreate(ECARTSTOP, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(ECARTSTOP, OBJPROP_CORNER, 1);
      ObjectSet(ECARTSTOP, OBJPROP_XDISTANCE, Xdist+23);
      ObjectSet(ECARTSTOP, OBJPROP_YDISTANCE,Ydist+15);
   }  
   ObjectSetText(ECARTSTOP, "EcartStop Indic:  "+DoubleToStr(iecartstop,NumRound)+"  ", Xfont, "Arial Black", LightSalmon);    

   if (ObjectFind(BANDSPERIOD) == -1)
   {
      ObjectCreate(BANDSPERIOD, OBJ_LABEL, 0, 0, 0, 0, 0); 
      ObjectSet(BANDSPERIOD, OBJPROP_CORNER, 1);
      ObjectSet(BANDSPERIOD, OBJPROP_XDISTANCE, Xdist+23);
      ObjectSet(BANDSPERIOD, OBJPROP_YDISTANCE,Ydist+30);  
   }
   ObjectSetText(BANDSPERIOD, "Bands Period:  "+BandsPeriod+"         ", Xfont, "Arial Black", LightSkyBlue);
   
   string EnvelopeMA, ExitOpposite;
   if(Envelope== true) EnvelopeMA= "ExitOnMA";
      else           EnvelopeMA=  "ExitOpposite";
      
   if (ObjectFind(RETURNTOMA) == -1)
   {
      ObjectCreate(RETURNTOMA, OBJ_LABEL, 0, 0, 0, 0, 0); 
      ObjectSet(RETURNTOMA, OBJPROP_CORNER, 3);
      ObjectSet(RETURNTOMA, OBJPROP_XDISTANCE, Xdist+160);
      ObjectSet(RETURNTOMA, OBJPROP_YDISTANCE,Ydist+45); 
   } 
   ObjectSetText(RETURNTOMA, "ReturnToMA: "+EnvelopeMA+"         ", Xfont, "Arial Black", Orchid);   
   
   if (ObjectFind(MOVINGAVERAGE) == -1)
   {
      ObjectCreate(MOVINGAVERAGE, OBJ_LABEL, 0, 0, 0, 0, 0); 
      ObjectSet(MOVINGAVERAGE, OBJPROP_CORNER, 1);
      ObjectSet(MOVINGAVERAGE, OBJPROP_XDISTANCE, Xdist);
      ObjectSet(MOVINGAVERAGE, OBJPROP_YDISTANCE,Ydist+45);  
   }
   ObjectSetText(MOVINGAVERAGE, "                         Moving Average:  "+MovingBufferMovingAverage[0], Xfont, "Arial Black", MediumPurple);
   
   if (ObjectFind(SPREADL) == -1)
   {
      ObjectCreate(SPREADL, OBJ_LABEL, 0, 0, 0, 0, 0);  
      ObjectSet(SPREADL, OBJPROP_CORNER, 1);
      ObjectSet(SPREADL, OBJPROP_XDISTANCE, Xdist);
      ObjectSet(SPREADL, OBJPROP_YDISTANCE,Ydist+60);  
   }
   ObjectSetText(SPREADL, "           Spread:  "+MovingBuffer[0], Xfont, "Arial Black", SteelBlue);

   if (ObjectFind(SPREADIFF) == -1)
   {
      ObjectCreate(SPREADIFF, OBJ_LABEL, 0, 0, 0, 0, 0);  
      ObjectSet(SPREADIFF, OBJPROP_CORNER, 1);
      ObjectSet(SPREADIFF, OBJPROP_XDISTANCE, Xdist);
      ObjectSet(SPREADIFF, OBJPROP_YDISTANCE,Ydist+75);  
   }
   ObjectSetText(SPREADIFF, "           DIfference:  "+(MovingBuffer[0]-MovingBufferMovingAverage[0]), Xfont, "Arial Black", Snow);
   
   if (ObjectFind(ECARTHIGH) == -1)
   {
      ObjectCreate(ECARTHIGH, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(ECARTHIGH, OBJPROP_CORNER, 1);
      ObjectSet(ECARTHIGH, OBJPROP_XDISTANCE, Xdist+240);
      ObjectSet(ECARTHIGH, OBJPROP_YDISTANCE,Ydist+70);  
   }
   ObjectSetText(ECARTHIGH, "Ecart High:  "+(MovingBufferMovingAverage[0]+Ecart)+"  ", Xfont, "Arial Black", Chocolate);     

   if (ObjectFind(ECARTLOW) == -1)
   {
      ObjectCreate(ECARTLOW, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(ECARTLOW, OBJPROP_CORNER, 1);
      ObjectSet(ECARTLOW, OBJPROP_XDISTANCE, Xdist+240);
      ObjectSet(ECARTLOW, OBJPROP_YDISTANCE,Ydist+85);  
   }
   ObjectSetText(ECARTLOW, "Ecart Low:   "+(MovingBufferMovingAverage[0]-Ecart)+"  ", Xfont, "Arial Black", NavajoWhite);    

   if (ObjectFind(CORRELATION) == -1)
   {   
      ObjectCreate (CORRELATION,OBJ_LABEL,0,0,MovingBufferMovingAverage[0]); 
      ObjectSet    (CORRELATION, OBJPROP_CORNER,2);   
      ObjectSet    (CORRELATION,OBJPROP_XDISTANCE,3);
      ObjectSet    (CORRELATION,OBJPROP_YDISTANCE,12);  
   }
   ObjectSetText(CORRELATION,"Spread: "+iTime(NULL,0,0),12,"Tahoma",Red);   
} 
//=====================================================================
   
   /*
   DrawIndicatorTrendLine(Time[100], 
                                 Time[50], 
                                 BandsDeviations,
                                 BandsDeviations, 
                                 Green, STYLE_SOLID);
   
   
   
   
   void DrawIndicatorTrendLine(datetime x1, datetime x2, double y1, 
                            double y2, color lineColor, double style)
   {
   int indicatorWindow = WindowFind(short_name);
   if(indicatorWindow < 0)
       return;
   string label = "MACD_DivergenceLine " + DoubleToStr(x1, 0);
   //ObjectDelete(label);
   ObjectCreate(label, OBJ_TREND, indicatorWindow, x1, y1, x2, y2, 
                0, 0);
   ObjectSet(label, OBJPROP_RAY, 0);
   ObjectSet(label, OBJPROP_COLOR, lineColor);
   ObjectSet(label, OBJPROP_STYLE, style);  
   }
   */                                  
//=====================================================================
/*
    Trend_Line(Time[10],Time[0],Open[10],Open[0],Gold,STYLE_SOLID);
    void Trend_Line(
    datetime x1, datetime x2, double y1, 
    double y2, color lineColor, double style)
    {
    //~~~~~~~~~~
    string label = "_Trend_Line_";
    ObjectDelete(label);
    ObjectCreate(label, OBJ_TREND, 1, x1, y1, x2, y2, 0, 0);
    ObjectSet(label, OBJPROP_RAY, 0);
    ObjectSet(label, OBJPROP_COLOR, lineColor);
    ObjectSet(label, OBJPROP_STYLE, style);
    //~~~~~~~~~~
    } 
*/   